<?php
namespace App\Service\H5;

class TaskService extends BaseService
{
    //数据列表
    public static function index($pas)
    {
        $db = self::dbConn(self::$dbYwTask)->where('del', 0);
       if (isset($pas['depart_id']) && $pas['depart_id']) {
           $db = $db->where('depart_id', $pas['depart_id']);
       }
        $res = $db->get();
        if (!count($res)) {
            return self::toback(-2, '没有数据');
        }
        $arys = [];
        foreach ($res as $v) {
//            $departOne = (array) self::dbConn(self::$dbStDepart)
//                ->where('id', $v->depart_id)
//                ->first();
//            $depart_name = empty($departOne) ? '' : $departOne['title'];
//            $pubOne = (array) self::dbConn(self::$dbStaff)
//                ->where('id', $v->staff_id)
//                ->first();
//            $pub_name = empty($pubOne) ? '' : $pubOne['title'];
            $arys[] = [
                'id' => $v->id, 'title' => $v->title, 
                'detail' => $v->detail, 'in_day' => $v->in_day,
//                'depart_id' => $v->depart_id, 'depart_name' => $depart_name,
//                'staff_id' => $v->staff_id, 'staff_name' => $pub_name,
            ];
        }
        return self::toback(0, '', $arys);
    }

    // 详情
    public static function getOne($id)
    {
        $res = (array) self::dbConn(self::$dbYwTask)
            ->select('id', 'title', 'detail', 'in_day')
            ->where('del', 0)
            ->where('id', $id)
            ->first();
        if (empty($res)) {
            // return self::toback(-2, '没有数据');
            $res = [
                'id' => 0, 'title' => '', 
                'detail' => '', 'in_day' => date('Y-m-d', time()),
            ];
        }
        return self::toback(0, '', $res);
    }

    //新增
    public static function add($pas)
    {
        $pas['addtime'] = $pas['uptime'] = toDate();
        $res = self::dbConn(self::$dbYwTask)->insertGetId($pas);
        if (!$res) {
            return self::toback(-1, 'error');
        }
        return self::toback();
    }

    //修改
    public static function modify($id, $pas)
    {
        $res = self::dbConn(self::$dbYwTask)->where('id', $id)->first();
        if (empty($res)) {
            return self::toback(-2, '数据不存在');
        }
        $pas['uptime'] = toDate();
        $res = self::dbConn(self::$dbYwTask)
            ->where('id', $id)
            ->update($pas);
        if (!$res) {
            return self::toback(-1, 'error');
        }
        return self::toback();
    }
//
//    //删除
//    public static function del($id)
//    {
//        $res = self::dbConn(self::$dbConfigItem)->where('id', $id)->first();
//        if (empty($res)) {
//            return self::toback(-2, '数据不存在');
//        }
//        $res = self::dbConn(self::$dbStDepart)
//            ->where('id', $id)
//            ->update(['del' => 1, 'deltime' => toDate()]);
//        if (!$res) {
//            return self::toback(-1, 'error');
//        }
//        return self::toback();
//    }
}